﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports System.Linq
Imports ISNet.WebUI.WebTreeView
Imports log4net


Partial Class mtCostCenter
    Inherits System.Web.UI.Page

    Public AlertText, ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("mtCostCenter")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Session("PlanYear") = "2557"
        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >> CostCenters"
        'Session("users") = "bb0011"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
            Session("SearchTxt") = ""
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
        Else
            'PlanYearsTxt.Text = Session("PlanYearTxt")
        End If

    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.CostCentersTableAdapter
        Dim dtTable As New MasterDataSet.CostCentersDataTable

        Dim sql As String = "select * from CostCenters "
        sql += " WHERE 1 = 1 "

        sql += Session("SearchTxt")

        sql += " ORDER BY PlanYear, CostCenterCode"

        adapter.SearchCostCenters(dtTable, sql)
        ' Response.Write(sql)

        Session("count_data") = dtTable.Rows.Count

        dtTable.Columns("PlanYear").AllowDBNull = True
        dtTable.Columns("CostCenterCode").AllowDBNull = True

        e.DataSource = dtTable

    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(3).ForceNoEdit = True
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New MasterDataSetTableAdapters.CostCentersTableAdapter
        Dim check = adapter.CheckExistCostCenter(Val(e.Row.Cells(1).Value), e.Row.Cells(2).Value, Session("FctCode"), Val(Session("PlanYearTxt")))

        If check <> 0 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            Dim code As String = ""
            code = Session("FctCode")

            If code = "" Then
                WebGrid1.ClearCachedDataSource()
                WebGrid1.ClientAction.Refresh()
                Throw New ArgumentException("กรุณาระบุ Fund Center")
            Else
                Dim id As Decimal

                id = adapter.InsertCostCenter(Val(e.Row.Cells(1).Value), e.Row.Cells(2).Value, Date.Now, Session("users"), Session("FctCode"), Val(Session("PlanYearTxt")))
                Dim dr As DataRow = e.Row.DataRow
                dr("id") = id

                logger.Info("User " & Session("users") & " insert CostCenters " & Val(e.Row.Cells(2).Value))
                WebGrid1.ClientAction.Refresh()
            End If
        End If
    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim id As String
        id = e.Row.KeyValue
        Dim adapter As New MasterDataSetTableAdapters.CostCentersTableAdapter
        Dim check = adapter.CheckExistUpdate(Val(e.Row.Cells(1).Value), e.Row.Cells(2).Value, Session("FctCode"), Val(Session("PlanYearTxt")), id)
        If check <> 0 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            Dim code As String = ""
            code = Session("FctCode")

            If code = "" Then
                WebGrid1.ClearCachedDataSource()
                WebGrid1.ClientAction.Refresh()
                Throw New ArgumentException("กรุณาระบุ Fund Center เพื่อทำการแก้ไข")
            Else
                adapter.UpdateCostCenter(Val(e.Row.Cells(1).Value), e.Row.Cells(2).Value, Date.Now, Session("users"), Session("FctCode"), Val(Session("PlanYearTxt")), id)
                WebGrid1.ClientAction.Refresh()

                logger.Info("User " & Session("users") & " update CostCenters " & Val(e.Row.Cells(2).Value))
            End If
        End If
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim adapter As New MasterDataSetTableAdapters.CostCentersTableAdapter

            If type = "1" Then
                ' MsgBox(Deletet)
                adapter.DeleteCostCenter(Deletet)
                logger.Info("User " & Session("users") & " delete CostCenters " & Deletet)
            ElseIf type = "2" Then
                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    adapter.DeleteCostCenter(keyValue)
                    logger.Info("User " & Session("users") & " delete CostCenters " & keyValue)
                Next keyValue
            End If
            WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        Session("PlanYearTxt") = PlanYearsTxt.Text
        Session("FctCode") = fctrWebCombo.Value

        If fctrWebCombo.Value <> "" And fctrWebCombo.Text <> "" And Val(fctrWebCombo.Value) <> 0 Then
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
            Session("SearchTxt") += " and FundCenter_FundCenterCode = '" & fctrWebCombo.Value & "'"
            WebGrid1.LayoutSettings.AllowAddNew = AddNew.Yes
        Else
            WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
            'Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
            L_CountData.Text = "กรุณาเลือก Fund Center"
        End If

        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub
    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub
End Class
